package cn.lopr.im.android.mvp.view.adapters;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.util.List;

import cn.lopr.im.android.R;
import cn.lopr.im.android.bean.HomeDialogueBean;
import cn.lopr.im.android.bean.User;
import cn.lopr.im.android.db.entity.Message;
import cn.lopr.im.android.mvp.view.adapters.base.DateComparator;
import cn.lopr.im.android.utils.DateUtil;
import cn.lopr.im.android.utils.EmojiUtil;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * @author shuangyang7
 * @date 2021/1/3 9:51
 */
public class HomeDialogueAdapter extends BaseAdapter {

    Context context;
    private List<User> friends;
    private List<HomeDialogueBean> homeDialogueBeans;
    CircleImageView ivPortrait;
    TextView tvNickname;
    TextView tvMsg;
    TextView tvDate;
    TextView tvMsgNum;
    DateComparator dateComparator;


    public HomeDialogueAdapter(FragmentActivity activity, List<User> friends, List<HomeDialogueBean> homeDialogueBeans) {
        this.context = activity;
        this.friends = friends;
        this.dateComparator = new DateComparator();
        this.homeDialogueBeans = homeDialogueBeans;
    }

    public void refresh(List<HomeDialogueBean> homeDialogueBeans) {
        this.homeDialogueBeans = homeDialogueBeans;
        notifyDataSetInvalidated();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return this.homeDialogueBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return this.homeDialogueBeans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        if (view == null) {
            view = View.inflate(context, R.layout.list_item_dialogue, null);
        }

        HomeDialogueBean homeDialogueBean = homeDialogueBeans.get(i);

        String fromPhone = homeDialogueBean.getFromPhone();
        User _friend = findFriends(fromPhone);

        if (friends == null || _friend == null) {
            return view;
        }

        ivPortrait = (CircleImageView) view.findViewById(R.id.iv_portrait);
        tvNickname = (TextView) view.findViewById(R.id.tv_nickname);
        tvMsg = (TextView) view.findViewById(R.id.tv_msg);
        tvDate = (TextView) view.findViewById(R.id.tv_date);
        tvMsgNum = (TextView) view.findViewById(R.id.tv_msg_num);

        int isReadNum = homeDialogueBean.getIsReadNum();

        Message lastMessage = homeDialogueBean.getLastMessage();

        if (lastMessage != null) {
            String lastMessageDate = lastMessage.getDate();
            boolean isCurrentDay = DateUtil.isCurrentDay(lastMessageDate);

            if (isCurrentDay) {
                Log.i("adapter", "getView: " + DateUtil.formatHMS(lastMessageDate) + "lastMessageDate : " + lastMessageDate);
                tvDate.setText(DateUtil.formatHMS(lastMessageDate));
            } else {
                tvDate.setText(DateUtil.formatDMY(lastMessageDate));
            }
            String check = "f0[0-9]{2}|f10[0-7]";               //正则达式，用来判断消息内是否有表情表

            SpannableString spannableString = EmojiUtil.getExpressionString(context, lastMessage.getMsg(), check);
            if (spannableString.length() > 50) {
                spannableString.subSequence(0, 50);
                spannableString.setSpan("...", 47, 50, SpannableString.SPAN_COMPOSING);
            }
            tvMsg.setText(spannableString);
        } else {
            tvDate.setText(DateUtil.formatHMS(homeDialogueBean.getLastUpdate()));
        }

        tvNickname.setText(_friend.getNickname());

        if (isReadNum != 0) {
            tvMsgNum.setText(String.valueOf(isReadNum));
        } else {
            tvMsgNum.setVisibility(View.GONE);
        }

        Glide.with(context).load(_friend.getPortrait()).placeholder(R.drawable.default_head_portrait)
                .dontAnimate()
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        ivPortrait.setImageDrawable(resource);
                    }

                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        super.onLoadFailed(e, errorDrawable);
                        ivPortrait.setImageDrawable(errorDrawable);
                    }
                });

        return view;
    }

    public User findFriends(String fromPhone) {
        for (User _friend : this.friends) {
            if (_friend.getPhone().equals(fromPhone)) {
                return _friend;
            }
        }

        return null;
    }

}
